﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Expression.Interactivity.Layout;
using System.Windows.Interactivity;
using System.Diagnostics;

namespace MyRun.Controls
{
    public class DragMoveBehavier : Behavior<FrameworkElement>
    {
        private FrameworkElement targetElement;

        public bool HasTargetComplete = false;
        protected override void OnAttached()
        {
            base.OnAttached();
            targetElement = this.AssociatedObject as FrameworkElement;
            targetElement.ManipulationDelta += new EventHandler<ManipulationDeltaEventArgs>(targetElement_ManipulationDelta);
            targetElement.ManipulationCompleted += new EventHandler<ManipulationCompletedEventArgs>(targetElement_ManipulationCompleted);
            HasTargetComplete = false;
        }

        public double CompleteValue
        {
            get
            {
                if (targetElement.RenderTransform is TranslateTransform)
                {
                    return (targetElement.RenderTransform as TranslateTransform).X;
                }
                else
                {
                    return 0;
                }
            }
            set
            {

                var targetTranslateTranform = targetElement.RenderTransform as TranslateTransform;
                targetTranslateTranform.X = value;
            }
        }

        public double CompleteWidth
        {
            get { return (double)GetValue(CompleteWidthProperty); }
            set { SetValue(CompleteWidthProperty, value); }
        }

        public static readonly DependencyProperty CompleteWidthProperty =
            DependencyProperty.Register("CompleteWidth", typeof(double), typeof(DragMoveBehavier), new PropertyMetadata(0.0));

        public event EventHandler TargetComplete;
        public event EventHandler TargetCompleting;

        void targetElement_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
        {
            var targetTranslateTranform = targetElement.RenderTransform as TranslateTransform;
            //if (targetTranslateTranform.X >= CompleteWidth)
            //{
            //    if (TargetComplete != null && HasTargetComplete == false)
            //    {
            //        TargetComplete(sender, e);
            //        HasTargetComplete = true;
            //    }
            //}
            //else
            //{
                targetTranslateTranform.X += e.DeltaManipulation.Translation.X;
           // }
        }

        void targetElement_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
        {
            var targetTranslateTranform = targetElement.RenderTransform as TranslateTransform;
            if (targetTranslateTranform.X < CompleteWidth)
            {
                if (TargetCompleting != null)
                {
                    TargetCompleting(sender, e);
                }
               
            }
            else
            {
                if (TargetComplete != null)
                {
                    TargetComplete(sender, e);
                }
            }
            targetTranslateTranform.X = 0;
        }


    }
}
